package com.itheima.job;

import com.itheima.job.config.SpringContextUtil;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author LiangTianQi
 * @describe    定时器任务，继承job接口
 * @date 2020/11/12 20:20
 */
@Component
public class OrderSettingJob implements Job {

    /*@Reference
    private OrderSettingService orderSettingService;*/
//    private OrderSettingService orderSettingService;

    /*@Autowired
    private JedisPool jedisPool;*/

    public void settingOrder() {
        //开启定时任务,每个月25日凌晨2点自动设置下个月每一天的可预约人数默认为300人
        //获取当前月份
        Calendar calendar = Calendar.getInstance();
        int month = calendar.get(Calendar.MONTH) + 1;
        //获取当前年份
        int year = calendar.get(Calendar.YEAR);

        //进行判断
        if (month < 12) {
            month=month+1;
        }else{
            year=year+1;
            month=1;
        }

        //进行字符串循环拼接
        int day=1;
        switch (month) {
            case 1: case 3: case 5:case 7:  case 8:  case 10:  case 12:
                day= 31;
                break;
            case 4:  case 6: case 9:  case 11:
                day= 30;
                break;
            //对于2月份需要判断是否为闰年
            case 2:
                if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
                    day= 29;
                    break;
                } else {
                    day= 28;
                    break;
                }
            default:
                day=0;
        }

        //打印当前时间
        Date date1 = new Date();
        String format = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss").format(date1);
        System.out.println(format);

        //进行循环添加数据
        for (int i = 1; i <= day; i++) {
            try {
                //转化成日期字符串
                String orderDate=year+"-"+month+"-"+i;
                System.out.println(orderDate);
                Date date = new SimpleDateFormat("yyyy-MM-dd").parse(orderDate);
                OrderSetting orderSetting = new OrderSetting();
                orderSetting.setOrderDate(date);
                /*将固定的默认人数改为从redis中读取到的默认人数
                出现问题reids不稳定，导致只能更改一些数据，后面的就不进行更新了，试一下放到数据库中
                orderSetting.setNumber(300);
                Jedis jedis = jedisPool.getResource();
                String number = jedis.get("number");
                int count=300;
                if (number != null && number != "") {
                    count= Integer.parseInt(number);
                }
                orderSetting.setNumber(count);
                System.out.println(orderSetting);*/

                GetOrderSettingImpl getOrderSettingImpl = (GetOrderSettingImpl) SpringContextUtil.getBean("getOrderSettingImpl");
                OrderSettingService orderSettingService = getOrderSettingImpl.getOrderSettingService();
                orderSettingService.triggerAdd(orderSetting);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }

    }

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        settingOrder();
    }
}
